import {
  isLeftSlide,
  isRightSlide,
  getCurrentPage,
  whenChangeMonth,
  renderCalendar,
  whenMulitSelect,
  whenSingleSelect,
  calculateNextWeekDays,
  calculatePrevWeekDays
} from './main.js';
var api = require("../../utils/api");
import request from "../../utils/request";
let currentPage = {};

Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    calendar: {
      type: Object
    }
  },
  lifetimes: {
    attached: function() {
      currentPage = getCurrentPage();
      var day2 = new Date();
      day2.setTime(day2.getTime());
      var month = (day2.getMonth() + 1) >= 10 ? (day2.getMonth() + 1) : "0" + (day2.getMonth() + 1)
      var fechTime = day2.getFullYear() + "" + month
      var newYear = day2.getFullYear()
      var newMonth = day2.getMonth() + 1
      this.fechDataFun(currentPage, newYear, newMonth, fechTime)
    }
  },
  attached: function() {
    currentPage = getCurrentPage();
  },
  methods: {
    // 请求数据
    fechDataFun(currentPage, newYear, newMonth, time) {
      var url = api.CalendarApi
      var params = {
        "access_token":"5b7f60aca7e7f6f8c680b1b219ad3ec6",
        "BADGENUMBER": wx.getStorageSync("userData").employeeId,
        "CHECKMOON": time
      }
      request(url, params, "POST").then(res => {
        if (res.code === "0" || res.code === 0) {
          renderCalendar.call(currentPage, newYear, newMonth, null, res.data);
          this.triggerEvent('myevent', { param: res.data.check_list})
          wx.setStorageSync("pageData", res.data)
        } else {
        }
      })
    },
    /**
     * 选择上一月
     */
    choosePrevMonth() {
      const { curYear, curMonth } = this.data.calendar;
      let newYear = curYear;
      let newMonth = curMonth - 1;
      if (newMonth < 1) {
        newYear = curYear - 1;
        newMonth = 12;
      }
      whenChangeMonth.call(currentPage, {
        curYear,
        curMonth,
        newYear,
        newMonth
      });
      currentPage.setData({
        'calendar.curYear': newYear,
        'calendar.curMonth': newMonth
      });
      var testdata = [];
      var fechTime = newYear + "" + (newMonth > 9 ? newMonth : "0" + newMonth)
      this.fechDataFun(currentPage, newYear, newMonth,fechTime)
    },
    /**
     * 选择下一月
     */
    chooseNextMonth() {
      const { curYear, curMonth } = this.data.calendar;
      let newYear = curYear;
      let newMonth = curMonth + 1;
      if (newMonth > 12) {
        newYear = curYear + 1;
        newMonth = 1;
      }
      whenChangeMonth.call(currentPage, {
        curYear,
        curMonth,
        newYear,
        newMonth
      });
      currentPage.setData({
        'calendar.curYear': newYear,
        'calendar.curMonth': newMonth
      });
      var fechTime = newYear + "" + (newMonth > 9 ? newMonth : "0" + newMonth)
      this.fechDataFun(currentPage, newYear, newMonth, fechTime)
      // renderCalendar.call(currentPage, newYear, newMonth,testdata);
    },
    /**
     * 日期点击事件
     * @param {!object} e 事件对象
     */
    tapDayItem(e) {
      this.triggerEvent('myevent', { param: wx.getStorageSync("pageData").calendar_list[e.currentTarget.dataset.idx].check_list })
      const { idx, disable } = e.currentTarget.dataset;
      if (disable) return;
      let currentSelected = {}; // 当前选中日期
      let { days, selectedDay: selectedDays } = this.data.calendar || []; // 所有选中日期
      const config = currentPage.config;
      const { multi, onTapDay } = config;
      const opts = {
        e,
        idx,
        onTapDay,
        currentSelected,
        selectedDays,
        days: days.slice()
      };
      if (multi) {
        whenMulitSelect.call(currentPage, opts);
      } else {
        whenSingleSelect.call(currentPage, opts);
      }
    },
    /**
     * 日历滑动开始
     * @param {object} e
     */
    calendarTouchstart(e) {
      const t = e.touches[0];
      const startX = t.clientX;
      const startY = t.clientY;
      currentPage.slideLock = true; // 滑动事件加锁
      currentPage.setData({
        'gesture.startX': startX,
        'gesture.startY': startY
      });
    },
    /**
     * 日历滑动中
     * @param {object} e
     */
    calendarTouchmove(e) {
      const self = currentPage;
      if (isLeftSlide.call(self, e)) {
        self.setData({
          'calendar.leftSwipe': 1
        });
        if (currentPage.weekMode)
          return calculateNextWeekDays.call(currentPage);
        this.chooseNextMonth();
      }
      if (isRightSlide.call(self, e)) {
        self.setData({
          'calendar.rightSwipe': 1
        });
        if (currentPage.weekMode)
          return calculatePrevWeekDays.call(currentPage);
        this.choosePrevMonth();
      }
    },
    calendarTouchend(e) {
      currentPage.setData({
        'calendar.leftSwipe': 0,
        'calendar.rightSwipe': 0
      });
    }
  }
});
